 package com.example.floristshop;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.floristshop.database.Flower;
import com.example.floristshop.database.Record;
import com.example.floristshop.utils.DBFindUtil;
import com.example.floristshop.utils.TimeUtil;

import java.util.Date;

 public class AddRecordActivity extends AppCompatActivity {
     Toolbar toolbar;
     Button submit_btn;
     LinearLayout flower_layout;
     TextView time_tv, flower_name_tv, user_name_tv;
     ImageView flower_image_iv;
     EditText record_number_et, record_money_et;

     String user_name;
     long timestamp, user_id, flower_id;
     int flower_image, number;
     double money;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_record);

        // init data
        timestamp = (System.currentTimeMillis())/1000L;
        SharedPreferences preferences = getSharedPreferences("user", MODE_PRIVATE);
        user_id = preferences.getLong("user_id", 10000);
        user_name = preferences.getString("user_name", "未登录用户");
        flower_id = 0;

        // get view
        toolbar = (Toolbar)findViewById(R.id.add_record_toolbar);
        submit_btn = (Button)findViewById(R.id.add_record_submit_button);
        flower_layout = (LinearLayout)findViewById(R.id.add_record_flower_info);
        time_tv = (TextView)findViewById(R.id.add_record_time);
        flower_name_tv = (TextView)findViewById(R.id.add_record_flower_name);
        user_name_tv = (TextView)findViewById(R.id.add_record_user_name);
        flower_image_iv = (ImageView)findViewById(R.id.add_record_flower_image);
        record_number_et = (EditText)findViewById(R.id.add_record_number);
        record_money_et = (EditText)findViewById(R.id.add_record_money);

        // set view
        time_tv.setText(TimeUtil.timestamp_to_timeString(timestamp));
        user_name_tv.setText(user_name);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {//toolbar的返回按钮
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });
        submit_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String numberString = record_number_et.getText().toString();
                String moneyString = record_money_et.getText().toString();
                if(flower_id==0){
                    Toast.makeText(AddRecordActivity.this, "尚未选择鲜花种类", Toast.LENGTH_SHORT).show();;
                }else if(numberString.equals("")||moneyString.equals("")){
                    Toast.makeText(AddRecordActivity.this, "数量/金额不能为空", Toast.LENGTH_SHORT).show();;
                }else{
                    number = Integer.parseInt(numberString);
                    money = Double.parseDouble(moneyString);
                    Record record = new Record(timestamp, number, money, flower_id, user_id);
                    record.save();
                    setResult(RESULT_OK);
                    finish();
                }
            }
        });
        flower_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AddRecordActivity.this, RecordSelectFlower.class);
                startActivityForResult(intent, 1);
            }
        });
    }

     @Override
     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
         switch (requestCode){
             case 1:
                 if(resultCode==RESULT_OK){
                     flower_id = data.getLongExtra("flower_id", 0);
                     Flower flower = DBFindUtil.findFlowerByFlowerId(flower_id);
                     flower_image_iv.setImageResource(flower.getImage());
                     flower_name_tv.setText(flower.getName());
                 }else{
                     flower_id = 0;
                 }
         }
     }
 }